package com.lineage.william;

import com.lineage.DatabaseFactory;
import com.lineage.server.datatables.SkillsTable;
import com.lineage.server.datatables.lock.CharSkillReading;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_AddSkill;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SkillSound;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1Skills;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class L1AutoLearnSkill {
  private static ArrayList<ArrayList<Object>> aData = new ArrayList<>();
  
  private static boolean NO_MORE_GET_DATA = false;
  
  public static final String TOKEN = ",";
  
  public static void forAutoLearnSkill(L1PcInstance pc) {
    ArrayList<?> aTempData = null;
    if (!NO_MORE_GET_DATA) {
      NO_MORE_GET_DATA = true;
      getData();
    } 
    int i = 0;
    while (i < aData.size()) {
      aTempData = aData.get(i);
      if (pc.getLevel() >= ((Integer)aTempData.get(0)).intValue()) {
        int[] id = (int[])aTempData.get(1);
        if (((Integer)aTempData.get(2)).intValue() == 0 && pc.isCrown()) {
          int l = 0;
          while (l < id.length) {
            int skillid = id[l];
            Skill(pc, skillid);
            l++;
          } 
        } else if (((Integer)aTempData.get(2)).intValue() == 1 && pc.isKnight()) {
          int l = 0;
          while (l < id.length) {
            int skillid = id[l];
            Skill(pc, skillid);
            l++;
          } 
        } else if (((Integer)aTempData.get(2)).intValue() == 2 && pc.isWizard()) {
          int l = 0;
          while (l < id.length) {
            int skillid = id[l];
            Skill(pc, skillid);
            l++;
          } 
        } else if (((Integer)aTempData.get(2)).intValue() == 3 && pc.isElf()) {
          int l = 0;
          while (l < id.length) {
            int skillid = id[l];
            Skill(pc, skillid);
            l++;
          } 
        } else if (((Integer)aTempData.get(2)).intValue() == 4 && pc.isDarkelf()) {
          int l = 0;
          while (l < id.length) {
            int skillid = id[l];
            Skill(pc, skillid);
            l++;
          } 
        } else if (((Integer)aTempData.get(2)).intValue() == 5 && pc.isDragonKnight()) {
          int l = 0;
          while (l < id.length) {
            int skillid = id[l];
            Skill(pc, skillid);
            l++;
          } 
        } else if (((Integer)aTempData.get(2)).intValue() == 6 && pc.isIllusionist()) {
          int l = 0;
          while (l < id.length) {
            int skillid = id[l];
            Skill(pc, skillid);
            l++;
          } 
        } 
      } 
      i++;
    } 
  }
  
  private static void getData() {
    Connection con = null;
    try {
      con = DatabaseFactory.get().getConnection();
      Statement stat = con.createStatement();
      ResultSet rset = stat.executeQuery("SELECT * FROM server_addskill");
      ArrayList<Object> aReturn = null;
      if (rset != null)
        while (rset.next()) {
          aReturn = new ArrayList();
          aReturn.add(0, new Integer(rset.getInt("Level")));
          if (rset.getString("SkillId") != null && !rset.getString("SkillId").equals("") && 
            !rset.getString("SkillId").equals("0")) {
            aReturn.add(1, getArray(rset.getString("SkillId"), ",", 1));
          } else {
            aReturn.add(1, null);
          } 
          aReturn.add(2, new Integer(rset.getInt("Class")));
          aData.add(aReturn);
        }  
      if (con != null && !con.isClosed())
        con.close(); 
    } catch (Exception exception) {}
  }
  
  private static void Skill(L1PcInstance pc, int skillid) {
    if (pc.isSkillMastery(skillid))
      return; 
    String s = "";
    int i = 0;
    int j = 0;
    int k = 0;
    int l = 0;
    int i2 = 0;
    int j2 = 0;
    int k2 = 0;
    int l2 = 0;
    int i3 = 0;
    int j3 = 0;
    int k3 = 0;
    int l3 = 0;
    int i4 = 0;
    int j4 = 0;
    int k4 = 0;
    int l4 = 0;
    int i5 = 0;
    int j5 = 0;
    int k5 = 0;
    int l5 = 0;
    int i6 = 0;
    int j6 = 0;
    int k6 = 0;
    int l6 = 0;
    int i7 = 0;
    int i8 = 0;
    int j7 = 0;
    int k7 = 0;
    int l7 = 0;
    L1Skills l1skills = SkillsTable.get().getTemplate(skillid);
    int l8 = l1skills.getSkillLevel();
    int i9 = l1skills.getId();
    s = l1skills.getName();
    i = l1skills.getSkillId();
    switch (l8) {
      case 1:
        j = i9;
        break;
      case 2:
        k = i9;
        break;
      case 3:
        l = i9;
        break;
      case 4:
        i2 = i9;
        break;
      case 5:
        j2 = i9;
        break;
      case 6:
        k2 = i9;
        break;
      case 7:
        l2 = i9;
        break;
      case 8:
        i3 = i9;
        break;
      case 9:
        j3 = i9;
        break;
      case 10:
        k3 = i9;
        break;
      case 11:
        l3 = i9;
        break;
      case 12:
        i4 = i9;
        break;
      case 13:
        j4 = i9;
        break;
      case 14:
        k4 = i9;
        break;
      case 15:
        l4 = i9;
        break;
      case 16:
        i5 = i9;
        break;
      case 17:
        j5 = i9;
        break;
      case 18:
        k5 = i9;
        break;
      case 19:
        l5 = i9;
        break;
      case 20:
        i6 = i9;
        break;
      case 21:
        j6 = i9;
        break;
      case 22:
        k6 = i9;
        break;
      case 23:
        l6 = i9;
        break;
      case 24:
        i7 = i9;
        break;
      case 25:
        j7 = i9;
        break;
      case 26:
        k7 = i9;
        break;
      case 27:
        l7 = i9;
        break;
      case 28:
        i8 = i9;
        break;
    } 
    int k8 = pc.getId();
    pc.sendPackets((ServerBasePacket)new S_AddSkill(j, k, l, i2, j2, k2, l2, i3, j3, k3, l3, i4, j4, k4, l4, i5, j5, k5, l5, i6, j6, 
          k6, l6, i7, j7, k7, l7, i8));
    S_SkillSound s_skillSound = new S_SkillSound(k8, 224);
    pc.sendPackets((ServerBasePacket)s_skillSound);
    pc.broadcastPacketX8((ServerBasePacket)s_skillSound);
    CharSkillReading.get().spellMastery(k8, i, s, 0, 0);
    pc.setSkillMastery(i);
    pc.sendPackets((ServerBasePacket)new S_ServerMessage(166, "学习了技能(" + s + ")"));
  }
  
  private static Object getArray(String s, String sToken, int iType) {
    StringTokenizer st = new StringTokenizer(s, sToken);
    int iSize = st.countTokens();
    String sTemp = null;
    if (iType == 1) {
      int[] iReturn = new int[iSize];
      int i = 0;
      while (i < iSize) {
        sTemp = st.nextToken();
        iReturn[i] = Integer.parseInt(sTemp);
        i++;
      } 
      return iReturn;
    } 
    if (iType == 2) {
      String[] sReturn = new String[iSize];
      int i = 0;
      while (i < iSize) {
        sTemp = st.nextToken();
        sReturn[i] = sTemp;
        i++;
      } 
      return sReturn;
    } 
    if (iType == 3) {
      String sReturn2 = null;
      int i = 0;
      while (i < iSize) {
        sTemp = sReturn2 = st.nextToken();
        i++;
      } 
      return sReturn2;
    } 
    return null;
  }
}
